package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.MerchantSettDetail;
import com.turing.post.organization.util.MerchantSettDetailDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "organization.impl.MerchantSettDetailDaoImpl")
@Remote
@Local
public class MerchantSettDetailDaoImpl implements MerchantSettDetailDao {

	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;
	
	@Override
	public void addMerchantSettDetail(MerchantSettDetail entity)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, entity);
	}

	@Override
	public void delMerchantSettDetail(MerchantSettDetail entity) throws PCPSApplicationException {
		GeneralDao.remove(_em, entity);
	}

	@SuppressWarnings("unchecked")
	@Override
	public MerchantSettDetail getMerchantSettDetailByID(BigInteger mrchSettDetailId) {
		String sql = "SELECT * FROM MERCHANTSETTDETAIL M " +
				"WHERE M.MRCHSETTDETAILID = ?1";
		Query query = _em.createNativeQuery(sql, MerchantSettDetail.class);
		query.setParameter(1, mrchSettDetailId);
		List<MerchantSettDetail> list = query.getResultList();
		if(list.size() > 0){
			return list.get(0);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public MerchantSettDetail getMerchantSettDetailForUpdate(BigInteger mrchSettDetailId) {
		String sql = "SELECT * FROM MERCHANTSETTDETAIL M " +
				"WHERE M.MRCHSETTDETAILID = ?1 FOR UPDATE";
		Query query = _em.createNativeQuery(sql, MerchantSettDetail.class);
		query.setParameter(1, mrchSettDetailId);
		List<MerchantSettDetail> list = query.getResultList();
		if(list.size() > 0){
			return list.get(0);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MerchantSettDetail> getMerchantSettDetailList(
			PCPSWebInput input, String merchantNo, String starDate, String endDate,
			String voucherNo, Integer page){
		int maxResult = Config.PAGE_SIZE;
		int firstPosition = maxResult * page;
		String sql = "SELECT * FROM MERCHANTSETTDETAIL M " +
				"WHERE M.TRANDATE >= TO_DATE('" + starDate + "', 'YYYY-MM-DD HH24:MI:SS') " +
						"AND M.TRANDATE <= TO_DATE('" + endDate + "', 'YYYY-MM-DD HH24:MI:SS') ";
		if(!PCPSUtil.isEmpty(merchantNo)){
			sql = sql + "AND M.MERCHANTNO = '" + merchantNo + "' ";
		}
		if(!PCPSUtil.isEmpty(voucherNo)){
			sql = sql + "AND M.VOUCHERNO = '" + voucherNo + "' ";
		}
		sql = sql + "ORDER BY M.TRANTIME DESC";
		Query query = _em.createNativeQuery(sql, MerchantSettDetail.class);
		query.setFirstResult(firstPosition);
		query.setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public BigInteger getMerchantSettDetailTotal(PCPSWebInput input,
			String merchantNo, String starDate, String endDate, String voucherNo){
		String sql = "SELECT NVL(COUNT(*), 0) " +
				"FROM MERCHANTSETTDETAIL M WHERE M.TRANDATE >= TO_DATE('" + starDate + "', 'YYYY-MM-DD HH24:MI:SS') AND M.TRANDATE <= TO_DATE('" + endDate + "', 'YYYY-MM-DD HH24:MI:SS') ";
		if(!PCPSUtil.isEmpty(merchantNo)){
			sql = sql + "AND M.MERCHANTNO = '" + merchantNo + "' ";
		}
		if(!PCPSUtil.isEmpty(voucherNo)){
			sql = sql + "AND M.VOUCHERNO = '" + voucherNo + "' ";
		}
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void modifyMerchantSettDetail(MerchantSettDetail entity)
			throws PCPSApplicationException {
		GeneralDao.update(_em, entity);
	}

}
